using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class FaucetItem : WaterDispenserItem
	{
		public enum Anim
		{
			Static = 0,
			Open = 1,
			Close = 2
		}

		public enum State
		{
			Close = 0,
			Open = 1
		}

		private const float CLOSE_ANGLE = 0f;

		private float OPEN_ANGLE = -90f;

		public State state;

		public Anim anim;

		private float animTimer;

		private float curVal;

		private float targetVal;

		private Transform faucet;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			faucet = model.Find("Faucet");
			if (faucet != null)
			{
				faucet.SetLocalZ(-1E-05f);
			}
			Transform trans = model.Find("Water");
			trans.SetLocalZ(1E-05f);
			if (!string.IsNullOrEmpty(base.itemData.scriptParams))
			{
				OPEN_ANGLE = int.Parse(base.itemData.scriptParams);
			}
			waterOutlet.gameObject.SetActive(false);
			SetState((State)data.state);
		}

		protected override void LateUpdate()
		{
			if (anim == Anim.Static)
			{
				return;
			}
			if (faucet == null)
			{
				anim = Anim.Static;
				return;
			}
			animTimer += Time.deltaTime * 2f;
			faucet.eulerAngles = new Vector3(0f, 0f, Mathf.Lerp(curVal, targetVal, animTimer));
			if (animTimer >= 1f)
			{
				anim = Anim.Static;
			}
		}

		private void SetState(State val)
		{
			if (state == val)
			{
				return;
			}
			state = val;
			if (state == State.Open)
			{
				waterOutlet.gameObject.SetActive(true);
				model.Find("Water").SetObjectActive(true);
				if (faucet != null)
				{
					faucet.eulerAngles = new Vector3(0f, 0f, OPEN_ANGLE);
				}
				Singleton<AudioManager>.Instance.PlayAudio("sound_water_2", true, GetAudioPlayer());
			}
			else
			{
				waterOutlet.gameObject.SetActive(false);
				model.Find("Water").SetObjectActive(false);
				if (faucet != null)
				{
					faucet.eulerAngles = new Vector3(0f, 0f, 0f);
				}
				RemoveAudioPlayer();
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (anim == Anim.Static)
			{
				animTimer = 0f;
				if (state == State.Close)
				{
					anim = Anim.Open;
					curVal = 0f;
					targetVal = OPEN_ANGLE;
					SetState(State.Open);
				}
				else if (state == State.Open)
				{
					anim = Anim.Close;
					curVal = OPEN_ANGLE;
					targetVal = 0f;
					SetState(State.Close);
				}
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
